package com.example.exam.util;

import com.example.exam.entity.ExamPaperAnswer;
import com.example.exam.service.CorrectExamPaperService;
import com.example.exam.service.ExamPaperAnswerService;
import lombok.extern.slf4j.Slf4j;
import org.quartz.Job;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.io.Serializable;

/**
 * @author shishuqian
 * date 2021/7/2
 * time 15:18
 **/
@Slf4j
@Component
public class ExamPaperAnswerUtil implements Job, Serializable {


    @Autowired
    private ExamPaperAnswerService examPaperAnswerService;

    @Autowired
    private CorrectExamPaperService correctExamPaperService;

    public static ExamPaperAnswerUtil examPaperAnswerUtil;

    @PostConstruct
    public void init(){
        examPaperAnswerUtil = this;
    }

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {

        JobDataMap jobDataMap = jobExecutionContext.getJobDetail().getJobDataMap();
        //Integer answerId = jobExecutionContext.getJobDetail().getJobDataMap().getIntValue("answerId");
        Integer answerId = jobDataMap.getIntValue("answerId");
        Boolean selectPeople = jobDataMap.getBoolean("selectPeople");
        log.info("取到了答卷的id:"+answerId);
        ExamPaperAnswer answer = examPaperAnswerUtil.examPaperAnswerService.findById(answerId);
        if (answer.getFinishStatus() == null || !answer.getFinishStatus()){ //考生在考试结束后也未结束考试
//            if (selectPeople != null && selectPeople){
//
//            }
            examPaperAnswerUtil.correctExamPaperService.systemCorrect(answerId);
        }
        log.info("已完成系统批改...");
    }
}
